<%--
  Created by IntelliJ IDEA.
  User: sunhao
  Date: 2016/7/8
  Time: 11:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>
<html>
<head>
    <title>活动列表</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%--<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">--%>
    <link href="/static/bui/css/bs3/dpl.css" rel="stylesheet" type="text/css"/>
    <link href="/static/bui/css/bs3/bui.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="doc-content">
    <div class="panel">
        <div class="panel-header">
            <h3>活动信息</h3>
        </div>

        <div class="doc-content">
            <div class="row">
                <div class="span21 offset3 control-row-auto">
                    <div id="grid"></div>
                    <input type="hidden" name="eduation">
                </div>
            </div>
        </div>


        <div id="content" class="hide">
            <form id="J_Form" class="form-horizontal bui-form-horizontal bui-form bui-form-field-container">
                <div class="row" style="display: none">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>_id：</label>

                        <div class="controls">
                            <input name="_id" type="text" id="_id" class="input-normal control-text bui-form-field"
                                   aria-disabled="false" aria-pressed="false">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>活动名称：</label>

                        <div class="controls">
                            <input id="name" name="name" type="text" data-rules="{required:true}"
                                   class="input-normal control-text bui-form-field" aria-disabled="false"
                                   aria-pressed="false">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label">活动状态：</label>

                        <div class="controls">
                            <select id="status" name="status" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="9" selected>请选择</option>
                                <option value="1">筹备</option>
                                <option value="0">结束</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>活动信息：</label>

                        <div class="controls">
                            <textarea id="info" name="info" aria-disabled="false" aria-pressed="false">
                            </textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <script type="text/javascript" src="/static/js/jquery.min.js"></script>
        <script type="text/javascript" src="/static/bui/js/bui.js"></script>
        <script type="text/javascript" src="/static/bui/js/config.js"></script>
        <script type="text/javascript" src="/static/js/cxdate.js"></script>
        <script type="text/javascript">
            BUI.use('common/page');
        </script>
        <script type="text/javascript">
            BUI.use(['bui/grid', 'bui/data', 'bui/form', 'bui/calendar'], function (Grid, Data, Form, Calendar) {
                var columns = [
                            {title: '活动名称', dataIndex: 'name', width: '25%'},
                            {title: '活动内容', dataIndex: 'info', width: '50%'},
                            {title: '状态', dataIndex: 'status', width: '10%'},
                            {title: '收入', dataIndex: 'income', width: '8%'},
                            {title: '支出', dataIndex: 'expend', width: '8%'},
                            {title: '利润', dataIndex: 'money', width: '8%'},
                            {
                                title: '操作', width: '20%', renderer: function () {
                                infoStr = '<span  class="grid-command btn-info"><i class="icon-info-sign"></i>详情</span>',
                                        editStr = '<span class="grid-command btn-edit"><i class="icon-edit"></i>编辑</span>'
                                return infoStr + editStr
                            }
                            }
                        ],
                //默认的数据
                        data = [
                            <c:forEach items="${active}" var="p">
                            {
                                _id: '${p._id}',
                                name: '${p.name}',
                                info: '${p.info}',
                                <%--registerTime: cxDate("YYYY-MM-DD", '${p.registerTime}'),--%>
                                status: '<c:if test="${p.status==1}">筹备</c:if><c:if test="${p.status==0}">结束</c:if>',
                                income: '${p.income}', expend: '${p.expend}', money: '${p.money}',
                            },
                            </c:forEach>
                        ],
                        store = new Data.Store({
                            data: data,
                            pageSize: 20
                        }),
                        datepicker = new Calendar.DatePicker({
                            trigger: '.calendar',
                            autoRender: true
                        });
                editing = new Grid.Plugins.DialogEditing({
                    contentId: 'content',
                    triggerCls: 'btn-edit',
                    editor: {
                        focusable: false,
                        success: function () {
                            var editType = editing.get('editType'),
                                    editor = this,
                                    form = editor.get("form");
                            form.valid();
                            if (form.isValid()) {
                                this.close();
                                var _id = $('#_id').val(),
                                        name = $('#name').val(),
                                        info = $('#info').val(),
                                        status = $('#status').val()
                                if (editType == 'add') {
                                    $.ajax({
                                        type: 'post',
                                        url: '/active/addActive',
                                        data: {"name": name, "info": info, "status": status},
                                        success: function (msg) {
                                            if (msg == "success") {
                                                msg = "添加成功！";
                                                BUI.Message.Alert(msg, function () {
                                                    window.location.href = '/active/list.do';
                                                }, 'success')
                                            } else {
                                                msg = "添加失败！";
                                                BUI.Message.Alert(msg, function () {
                                                    window.location.href = '/jsp/error/noAuthority.jsp';
                                                }, 'error')
                                            }

                                        }
                                    })
                                } else {
                                    $.ajax({
                                        type: 'post',
                                        url: '/active/updateActive',
                                        data: {"_id": _id, "name": name, "info": info, "status": status},
                                        success: function (msg) {
                                            if (msg == 'success') {
                                                msg = '修改成功！';
                                                BUI.Message.Alert(msg, function () {
                                                    window.location.href = '/active/list.do';
                                                }, 'success')
                                            } else {
                                                msg = '修改失败！';
                                                BUI.Message.Alert(msg, function () {
                                                    window.location.href = '/jsp/error/noAuthority.jsp';
                                                }, 'error')
                                            }
                                        }
                                    })
                                }
                            }
                        }
                    }
                }),
                        grid = new Grid.Grid({
                            render: '#grid',
                            columns: columns,
                            width: '100%',
//                    forceFit: true,
                            plugins: [Grid.Plugins.AutoFit],
                            store: store,
                            bbar: {
                                pagingBar: true,
                                elCls: 'image-pbar'
                            },
                            plugins: [Grid.Plugins.CheckSelection, editing],
                            tbar: {
                                items: [{
                                    btnCls: 'button button-small',
                                    text: '<i class="icon-plus"></i>添加',
                                    listeners: {
                                        'click': addFunction
                                    }
                                },
                                    {
                                        btnCls: 'button button-small',
                                        text: '<i class="icon-remove"></i>删除',
                                        listeners: {
                                            'click': delFunction
                                        }
                                    }]
                            }
                        });
                grid.render();

                grid.on('cellclick', function (ev) {
                    var record = ev.record,
                            target = $(ev.domTarget);
                    if (target.hasClass('btn-info')) {
                        window.location.href = '/active/detail?activeId=' + record._id;
                    }
                })

                function addFunction() {
                    var newData = {};
                    editing.add(newData); //添加记录后，直接编辑
                }

                function delFunction() {
                    var selections = grid.getSelection();
                    delItems(selections);
                }

                function delItems(items) {
                    var _ids = [];
                    BUI.each(items, function (item) {
                        _ids.push(item._id)
                    });
                    if (_ids.length) {
                        BUI.Message.Confirm('确认要删除吗？', function () {
                            $.ajax({
                                type: 'POST',
                                url: '/active/delActive?_ids=' + _ids,
                                success: function (msg) {
                                    if (msg == 'success') {
                                        msg = "删除成功！";
                                    }
                                    BUI.Message.Alert(msg, function () {
                                        window.location.href = '/active/list.do';
                                    }, 'success')
                                }
                            })
                        })
                    }
                };
                var form = new Form.HForm({
                    srcNode: '#J_Form'
                });
            });
        </script>
    </div>
</div>
</body>
</html>
